package com.bailan.controller;

import com.bailan.pojo.Admin;
import com.bailan.pojo.Student;
import com.bailan.pojo.Teacher;
import com.bailan.service.ITeacherService;
import com.bailan.utils.CheckUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiLan
 * @since 2022:05:24
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    private ITeacherService teacherService;

    @Autowired
    public void setTeacherService(ITeacherService teacherService) {
        this.teacherService = teacherService;
    }

    @PostMapping(consumes = "application/json")
    @ResponseBody
    public String changPassword(@RequestBody Map<String,String> map) {

        System.out.println(map);
        Teacher user = CheckUser.getInstance(teacherService);
        System.out.println(user);
        user.setId(map.get("id"));
        user.setPassword(map.get("password"));
        Teacher check = CheckUser.check(teacherService, user);

        if (Objects.nonNull(check)){
            check.setPassword(map.get("newPassword"));
            teacherService.updateById(check);
            return "succeed";
        }

        return "false";
    }



    @GetMapping(consumes = "application/json")
    @ResponseBody
    public Teacher getInfo(@RequestBody Teacher user){
        LambdaQueryWrapper<Teacher> LambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper
                .in(Teacher::getId,user.getId());
        Teacher one = teacherService.getOne(LambdaQueryWrapper);
        System.out.println(one);
        if (Objects.nonNull(one)){
            return one;
        }else {
            return null;
        }
    }
}
